/**
 * kl yy-ds @2021
 */
package com.cxps.server.product.controller;

import com.cxps.cloud.message.ApiResponse;
import com.cxps.server.product.domain.AddCartForm;
import com.cxps.server.product.domain.ProductQuery;
import com.cxps.server.product.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * 商品列表 前端控制器
 *
 * @author YouCai.Liu
 * @since 2021/11/12
 * ...
 */
@Slf4j
@RestController
@Api(tags = "商品列表API")
@RequestMapping("/retail/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 商品列表数据
     *
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "商品列表数据", response = ApiResponse.class)
    public ApiResponse list(@RequestBody ProductQuery query) throws IOException {
        try {
            Assert.notNull(query, "Query不能为空");
            return ApiResponse.fireSuccess(productService.list(query));
        } catch (IllegalArgumentException e) {
            log.error("商品列表数据查询失败：" + e.getMessage(), e);
            return ApiResponse.fireError(e.getMessage());
        }
    }

    /**
     * 获取查询条件
     *
     * @return
     * @throws IOException
     */
    @GetMapping("/getSearchCondition")
    @ApiOperation(value = "获取查询条件", response = ApiResponse.class)
    public ApiResponse getSearchCondition() {
        try {
            return ApiResponse.fireSuccess(productService.getSearchCondition());
        } catch (IllegalArgumentException e) {
            log.error("获取查询条件失败：" + e.getMessage(), e);
            return ApiResponse.fireError(e.getMessage());
        }
    }

    /**
     * 商品收藏
     *
     * @return
     */
    @GetMapping("/collectProduct")
    @ApiOperation(value = "商品收藏", response = ApiResponse.class)
    public ApiResponse collectProduct(@RequestParam String bizId) {
        try {
            return ApiResponse.fireSuccess(productService.collectProduct(bizId));
        } catch (IllegalArgumentException e) {
            log.error("获取查询条件失败：" + e.getMessage(), e);
            return ApiResponse.fireError(e.getMessage());
        }
    }

    /**
     * 加入购物车
     *
     * @return
     */
    @PostMapping("/addCart")
    @ApiOperation(value = "加入购物车", response = ApiResponse.class)
    public ApiResponse addCart(@RequestBody AddCartForm from) {
        try {
            Assert.notNull(from, "添加购物车操作对象不能为空");
            Assert.hasLength(from.getUserId(), "用户ID不能为空");
            Assert.hasLength(from.getProductId(), "商品ID不能为空");
            productService.addCart(from);
            return ApiResponse.fireSuccess();
        } catch (IllegalArgumentException e) {
            log.error("加入购物车失败：" + e.getMessage(), e);
            return ApiResponse.fireError(e.getMessage());
        }
    }

    /**
     * 购物车商品数量操作
     *
     * @return
     */
    @PostMapping("/addCart2")
    @ApiOperation(value = "加入购物车", response = ApiResponse.class)
    public ApiResponse addCart2(@RequestBody AddCartForm from) {
        try {
            Assert.notNull(from, "添加购物车操作对象不能为空");
            Assert.hasLength(from.getUserId(), "用户ID不能为空");
            Assert.hasLength(from.getProductId(), "商品ID不能为空");
            Assert.notNull(from.getProductNum(), "商品数量不能为空");
            productService.addCart2(from);
            return ApiResponse.fireSuccess();
        } catch (IllegalArgumentException e) {
            log.error("加入购物车失败：" + e.getMessage(), e);
            return ApiResponse.fireError(e.getMessage());
        }
    }

    /**
     * 获取购物车列表
     *
     * @return
     */
    @GetMapping("/getCart/{userId}")
    @ApiOperation(value = "获取购物车列表", response = ApiResponse.class)
    public ApiResponse getCart(@PathVariable String userId) throws IOException {
        try {
            return ApiResponse.fireSuccess(productService.getCart(userId));
        } catch (IllegalArgumentException e) {
            log.error("加入购物车失败：" + e.getMessage(), e);
            return ApiResponse.fireError(e.getMessage());
        }
    }


    /**
     * 去结算
     *
     * @param userId
     * @return
     * @throws IOException
     */
    @GetMapping("/goShopping")
    @ApiOperation(value = "去结算", response = ApiResponse.class)
    public ApiResponse goShopping(@RequestParam String userId, @RequestParam String[] selectProductId) throws IOException {
        try {
            return ApiResponse.fireSuccess(productService.getCart(userId));
        } catch (IllegalArgumentException e) {
            log.error("加入购物车失败：" + e.getMessage(), e);
            return ApiResponse.fireError(e.getMessage());
        }
    }

    /**
     * 计算价格
     *
     * @param userId
     * @return
     * @throws IOException
     */
    @GetMapping("/jsjg")
    @ApiOperation(value = "计算价格", response = ApiResponse.class)
    public ApiResponse jsjg(@RequestParam String userId, @RequestParam String[] selectProductId) throws IOException {
        try {
            return ApiResponse.fireSuccess(productService.jsjg(userId,selectProductId));
        } catch (IllegalArgumentException e) {
            log.error("加入购物车失败：" + e.getMessage(), e);
            return ApiResponse.fireError(e.getMessage());
        }
    }


    /**
     * 提交订单
     * @param userId
     * @param shdz
     * @param productId
     * @param yhj
     * @return
     * @throws IOException
     */
    @PostMapping("/submitOrder")
    @ApiOperation(value = "提交订单", response = ApiResponse.class)
    public ApiResponse submitOrder(@RequestParam String userId, @RequestParam String shdz,
                                   @RequestParam String[] productId, @RequestParam String yhj) throws IOException {
        Executors.newFixedThreadPool(5);
        try {
            return ApiResponse.fireSuccess(productService.getCart(userId));
        } catch (IllegalArgumentException e) {
            log.error("加入购物车失败：" + e.getMessage(), e);
            return ApiResponse.fireError(e.getMessage());
        }
    }



}
